<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 支付异常类 写入支付日志
 * Class Pay_Exception
 */
class Pay_Exception extends Exception
{
    //日志目录
    private $_message;
    private $_code;

    //异常构造函数
    public function __construct($message = "", $code = 0, $log_dir='')
    {
        parent::__construct($message, $code);
        $this->_message = $message;
        $this->_code = $code;
        $this->write_log($log_dir);
    }

    //写入日志
    function write_log($log_dir)
    {
        $base_path = rtrim(BASEPATH, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR .
            'data' . DIRECTORY_SEPARATOR .
            'logs' . DIRECTORY_SEPARATOR .
            'payment' . DIRECTORY_SEPARATOR;
        $log_pay_dir = $base_path . $log_dir . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR;

        if (! file_exists($log_pay_dir))
        {
            mkdir($log_pay_dir, 0666,true);
        }
        $file = $log_pay_dir . date('Ymd') . '.php';
        $time = date('Y-m-d H:i:s');
        $errorFile = $this->getFile();
        $line = $this->getLine();
        $message = $this->getMessage();
        $logFormat = <<<LOG
#time:$time
#message:$message
#file:$errorFile [$line]
LOG;
        file_put_contents($file, PHP_EOL . $logFormat . PHP_EOL, FILE_APPEND);
    }

}